from PyQt5.QtCore import Qt
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QFileDialog, QGraphicsPixmapItem, QGraphicsScene, QMainWindow, QMessageBox
from Ui_mainwindow import Ui_MainWindow
import numpy as np
import cv2
import util


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.img = None  # 读入图片
        self.scene = QGraphicsScene()  # 场景
        self.pic_item = QGraphicsPixmapItem()  # 渲染的图片物件
        self.ui.graphicsView.setScene(self.scene)

        self.ui.actionchooseImage.triggered.connect(self.chooseImage)
        self.ui.btnConfirm.clicked.connect(self.resizeImage)
        self.ui.actionresetPic.triggered.connect(self.resetImage)
        self.ui.actionsavePic.triggered.connect(self.saveImage)

    def reset(self):
        self.scene.clear()
        self.img = None
        self.ui.WidthLine.setText("0")
        self.ui.HeightLine.setText("0")

    def resetImage(self):
        if self.img is None:
            return

        self.drawImage(self.img)

    def chooseImage(self):
        path, filetype = QFileDialog.getOpenFileName(
            self, "选择图片", ".", "Image Files(*.jpg *.png)")

        if path == "":
            return

        self.reset()
        self.img = QImage()
        if self.img.load(path) == False:
            QMessageBox.critical(self, "错误", "图像文件无法打开！")
            self.img = None
            return

        self.drawImage(self.img)

    def drawImage(self, image: QImage):
        self.scene.clear()

        self.ui.WidthLine.setText(str(image.width()))
        self.ui.HeightLine.setText(str(image.height()))

        img_pixmap = QPixmap.fromImage(image)

        self.pic_item = QGraphicsPixmapItem()
        self.pic_item.setPixmap(img_pixmap)
        self.pic_item.setPos(0, 0)
        # 居中显示
        self.scene.setSceneRect(self.pic_item.sceneBoundingRect())
        self.scene.addItem(self.pic_item)

    def saveImage(self):
        if self.img is None:
            return

        path, filetype = QFileDialog.getSaveFileName(
            self, "存储图像", ".", "Image Files(*.jpg *.png *.bmp")

        if path == "":
            return

        if self.pic_item.pixmap().save(path) == False:
            QMessageBox.critical(self, "错误", "存储图像失败！")

    def resizeImage(self):
        if self.img is None:
            return

        index = self.ui.comboBox.currentIndex()
        if index == 0:
            self.toSOneInch()
        elif index == 1:
            self.toOneInch()
        elif index == 2:
            self.toLOneInch()
        elif index == 3:
            self.toSTwoInch()
        elif index == 4:
            self.toTwoInch()
        elif index == 5:
            self.toGivenInch()
        else:
            return

    def toSOneInch(self):
        image = self.img.scaled(
            259, 377, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
        self.drawImage(image)

    def toOneInch(self):
        image = self.img.scaled(
            295, 413, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
        self.drawImage(image)

    def toLOneInch(self):
        image = self.img.scaled(
            390, 567, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
        self.drawImage(image)

    def toSTwoInch(self):
        image = self.img.scaled(
            413, 531, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
        self.drawImage(image)

    def toTwoInch(self):
        image = self.img.scaled(
            413, 626, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
        self.drawImage(image)

    def toGivenInch(self):
        image = self.img.scaled(
            int(self.ui.WidthLine.text()), int(self.ui.HeightLine.text()), Qt.KeepAspectRatio, Qt.SmoothTransformation)
        self.drawImage(image)
